package cn.edu.sdjzu.xg.eagle.service.impl;

import cn.edu.sdjzu.xg.eagle.dao.DepartmentDao;
import cn.edu.sdjzu.xg.eagle.dao.mapper.DepartmentDaoImpl;
import cn.edu.sdjzu.xg.eagle.entity.Department;
import cn.edu.sdjzu.xg.eagle.entity.School;
import cn.edu.sdjzu.xg.eagle.exception.BaseException;
import cn.edu.sdjzu.xg.eagle.factory.DaoFactory;
import cn.edu.sdjzu.xg.eagle.service.DepartmentService;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;

/**
 * @author tg.si@188.com
 * @project xg23
 * @date 2025/3/27
 */
public class DepartmentServiceImpl implements DepartmentService {

    public Collection<Department> findAll() throws SQLException, BaseException, InvocationTargetException {
        return DaoFactory.getDao(DepartmentDao.class, "DepartmentDaoImpl").findAll();
    }

    public Collection<Department> getAll(School school) throws SQLException, BaseException, InvocationTargetException {
        Collection<Department> departments = new HashSet<Department>();
        for (Department department : DaoFactory.getDao(DepartmentDaoImpl.class, "DepartmentDaoImpl").findAll()) {
            if (department.getSchool() == school) {
                departments.add(department);
            }
        }
        return departments;
    }

    @Override
    public int countAll(School school) throws SQLException, BaseException {
        return DaoFactory.getDao(DepartmentDao.class,"DepartmentDaoImpl").count(school);
    }

    @Override
    public boolean add(Department department) throws SQLException, BaseException {
        return DaoFactory.getDao(DepartmentDao.class,"DepartmentDaoImpl").add(department);
    }

    @Override
    public boolean delete(Integer id) throws SQLException, BaseException {
        return DaoFactory.getDao(DepartmentDao.class,"DepartmentDaoImpl").delete(id);
    }

    @Override
    public boolean delete(Department department) throws SQLException, BaseException {
        return DaoFactory.getDao(DepartmentDao.class,"DepartmentDaoImpl").delete(department);
    }

    @Override
    public Department find(Integer id) throws SQLException, BaseException, InvocationTargetException {
        return DaoFactory.getDao(DepartmentDao.class,"DepartmentDaoImpl").find(id);
    }


}
